import math
class Solution(object):
    def replaceNonCoprimes(self, nums):
        st = [nums[0]]
        n = len(nums)
        for i in range(1, n):
            st.append(nums[i])
            while len(st) >= 2:
                g = math.gcd(st[-1], st[-2])
                if g > 1:
                    num = st.pop()
                    st[-1] = st[-1] * num // g
                else:
                    break
        return st
data = Solution()
nums = [6,4,3,2,7,6,2]
nums = [287,41,49,287,899,23,23,20677,5,825]
print(data.replaceNonCoprimes(nums))